package com.hongyuan.sellfitness.ui.home.activity;

import android.os.Bundle;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivitySignupGroupBinding;
import com.hongyuan.sellfitness.ui.home.view_mode.SignUpGroupViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class SignUpGroupActivity extends CustomActivity<ActivitySignupGroupBinding, SignUpGroupViewModel> {

    private List<Integer> list;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_signup_group;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SignUpGroupViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SignUpGroupViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.comBack.setOnClickListener(view -> finish());
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.clearParams().setParams("cs_id",getBundle().getString("cs_id"));

        //获取客服电话
        viewModel.clearParams().setParams("type","pt");
        viewModel.requestData(Constants.GET_ONLINE_KF);
        viewModel.requestData(Constants.GET_SOURSE_SUPER_INFO);
        //获取团课位置
        viewModel.requestData(Constants.GET_CS_NUMBER);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.setSites.observe(this, integers -> {
            this.list = integers;
        });

        viewModel.uc.showSelectSite.observe(this, unitBeanUtils -> {
            CustomDialog.scroller(this, unitBeanUtils.getUnitList(list), "选择位置", (v1, message,id) -> {
                binding.locatNum.setText(message);
            });
        });

        viewModel.uc.goNext.observe(this, aVoid -> {
            if(viewModel.entity.get().getIs_select_num() == 1 && "请选择".equals(binding.locatNum.getText().toString())){
                ToastUtils.showShort("请选择位置");
                return;
            }

            if(viewModel.entity.get().getIs_select_num() == 1){
                viewModel.setParams("ocs_number",binding.locatNum.getText().toString());
            }

            viewModel.requestData(Constants.SIGIN_UP_COURSE_SUPER);
        });
    }
}
